package edu.gatech.bsbmule.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.colorchooser.AbstractColorChooserPanel;

/**
 * The GUI that enables players to configure their own attributes
 * 
 * @author Vania, Hanjie, Melissa, Manav
 * @version 1.0 10/6/2013
 * 
 */
public class PlayerConfig extends JPanel implements IPlayerConfig {

	private JLabel lblMule;
	private JTextField textFieldName;
	private JComboBox<String> comboBoxRace;
	private JColorChooser colorChooser;
	// private ColorSelectionModel model;
	private JButton btnNext;
	private JSplitPane splitPane2;

	/**
	 * Create the panel.
	 */
	public PlayerConfig() {
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		lblMule = new JLabel("Player #0");
		lblMule.setFont(new Font("Tahoma", Font.PLAIN, 36));
		lblMule.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblMule);

		JSplitPane splitPane0 = new JSplitPane();
		splitPane0.setEnabled(false);
		add(splitPane0);
		splitPane0.setDividerLocation(300);
		JLabel numPlayersLab = new JLabel("Name");
		numPlayersLab.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane0.setLeftComponent(numPlayersLab);

		textFieldName = new JTextField();
		textFieldName.setFont(new Font("Tahoma", Font.PLAIN, 16));
		splitPane0.setRightComponent(textFieldName);
		textFieldName.setColumns(10);

		JSplitPane splitPane1 = new JSplitPane();
		splitPane1.setEnabled(false);
		add(splitPane1);
		splitPane1.setDividerLocation(300);
		JLabel difficultyLvlLab = new JLabel("Race");
		difficultyLvlLab.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane1.setLeftComponent(difficultyLvlLab);

		comboBoxRace = new JComboBox<String>();
		comboBoxRace.setFont(new Font("Tahoma", Font.PLAIN, 16));
		comboBoxRace.setModel(new DefaultComboBoxModel(new String[] {"Flapper (easiest)", "Human (hardest)", "Others (normal)"}));
		splitPane1.setRightComponent(comboBoxRace);

		splitPane2 = new JSplitPane();
		splitPane2.setEnabled(false);
		add(splitPane2);
		splitPane2.setDividerLocation(300);
		JLabel typeOfMapLab = new JLabel("Color");
		typeOfMapLab.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane2.setLeftComponent(typeOfMapLab);

		colorChooser = new JColorChooser();
		AbstractColorChooserPanel[] panels = colorChooser.getChooserPanels();
        for (AbstractColorChooserPanel accp : panels) {
            if (accp.getDisplayName().equals("RGB")) {
                panels[0] = accp;
            }
        }
        colorChooser.setChooserPanels(panels);
		splitPane2.setRightComponent(colorChooser);

		JSplitPane splitPane3 = new JSplitPane();
		splitPane3.setEnabled(false);
		add(splitPane3);
		splitPane3.setDividerLocation(300);

		btnNext = new JButton("NEXT");
		btnNext.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane3.setLeftComponent(btnNext);

		JButton btnClear = new JButton("CLEAR");
		btnClear.addMouseListener(new MouseAdapter() {

			@Override
			/**
			 * Clear all the values in number of players, difficulty level, 2type of map and number of turns
			 */
			public void mouseClicked(MouseEvent e) {
				textFieldName.setText("");
				comboBoxRace.setSelectedItem("Flapper (easiest)");
			}

		});
		btnClear.setFont(new Font("Tahoma", Font.PLAIN, 24));
		splitPane3.setRightComponent(btnClear);
	}
	
	/**
	 * @see edu.gatech.bsbmule.view.IPlayerConfig
	 */
	@Override
	public String getPlayerName() {
		return textFieldName.getText();
	}
	
	/**
	 * @see edu.gatech.bsbmule.view.IPlayerConfig
	 */
	@Override
	public int getPlayerRace() {
		return comboBoxRace.getSelectedIndex();
	}
	
	/**
	 * @see edu.gatech.bsbmule.view.IPlayerConfig
	 */
	@Override
	public Color getPlayerColor() {
		// TODO: fix this
		return colorChooser.getColor();
	}
	
	/**
	 * @see edu.gatech.bsbmule.view.IPlayerConfig
	 */
	@Override
	public void setOKButtonListener(MouseAdapter e) {
		btnNext.addMouseListener(e);
	}
	
	/**
	 * @see edu.gatech.bsbmule.view.IPlayerConfig
	 */
	@Override
	public void setSequence(int i) {
		lblMule.setText("Player #" + i);		
	}
	
	/**
	 * @see edu.gatech.bsbmule.view.IPlayerConfig
	 */
	@Override
	public void reset() {
		textFieldName.setText("");
		comboBoxRace.setSelectedIndex(0);
		colorChooser.setColor(Color.WHITE);
	}
	
}
